06. Exercise: Test AdapterViews with Espresso

Test AdapterViews with Espresso

In this exercise you'll finish building the MenuActivityScreenTest, which demos a user clicking on a GridView item in MenuActivity, and opens up the corresponding OrderActivity.

Exercise Code

Exercise: TESP.02-Exercise-AddMenuActivityScreenTest


AdapterViews include ListViews and GridViews

AdapterViews include ListViews and GridViews

While onView() can handle most Views in our UI, Espresso does require a different method call when dealing with AdapterView widgets. Since AdapterViews such as ListView and GridView load data dynamically from an Adapter, only a subset of the contents may be loaded in the current view hierarchy at a time. This means that onView() may not be able to find the necessary view.

To handle this we need to use onData() which loads the adapter item we are interested in onto the screen before operating on it.

To help us further specify the item in the AdapterView we’re interested in, we can use a DataOption method such as inAdapterView() or atPosition(). These methods are highlighted in the cheat sheet below.

After that, we perform an action and then check that we get the desired result on the view that we’re interested in.

Notice that how we test Views in AdapterViews is very similar to how we test single Views - matching, acting, and asserting.

As you may have guessed, the perfect place to try this out is in the GridView of MenuActivity of TeaTime!

One test I’d like to run is clicking on a gridView tea item and checking that it opens up the corresponding tea OrderActivity.

For this exercise, start by downloading the code from TESP.02-Exercise-AddMenuActivityScreenTest.

You’ll see that I’ve added the skeleton of MenuActivityScreenTest.java and some TODOs.

This time I’ll give you fewer hints, since I want to you to explore how to perform a click action on a gridView item by yourself. Then you’ll check that it opened up the correct OrderActivity by verifying the tea name text view.

Check out the resources below if you need a hint about which methods to use in order to specify specific AdapterView items.

Resources

Task Description:

Follow the TODOs to complete this exercise.

Task List:

Task Feedback:

Nice work! We've covered 2 types of Espresso tests so far. Stay tuned for more.

Solution: [ESP.02-Solution-AddMenuActivityScreenTest][Diff]